from Settings import *
import pygame
from random import random,randint
from math import ceil



def gen_map():
    images = [pygame.image.load(tile) for tile in GamePath.ground]
    images = [pygame.transform.scale(image, (SceneSettings.tileWidth, SceneSettings.tileHeight)) for image in images]
    hei = pygame.image.load(GamePath.void[0])
    hei = pygame.transform.scale(hei, (SceneSettings.tileWidth, SceneSettings.tileHeight))
    wall = [pygame.image.load(tile) for tile in GamePath.wall]
    wall = [pygame.transform.scale(wall, (SceneSettings.tileWidth, SceneSettings.tileHeight)) for wall in wall]
    obstacle = [pygame.image.load(tile) for tile in GamePath.obstacle]
    obstacle = [pygame.transform.scale(obstacle, (SceneSettings.tileWidth, SceneSettings.tileHeight)) for obstacle in obstacle]

    mapObject = []
    bianjie = []
    #generate map
    for i in range(len(MapSettings.matrix)):
        tmp = []
        for j in range(len(MapSettings.matrix[i])):
            if MapSettings.matrix[i][j] == 0:
                tmp.append(hei)
            elif MapSettings.matrix[i][j] == 1:
                tmp.append(wall[randint(0, len(wall)-1)])
                bianjie.append((i,j))
            elif MapSettings.matrix[i][j] == 2: 
                tmp.append(images[randint(0, len(images)-1)])
                
            
        mapObject.append(tmp)

    return [mapObject,bianjie]        
def build_obstacle():
    #在地图上随机生成障碍物
    mapObject = gen_map()[0]
    bianjie = gen_map()[1]
    obstacle = pygame.image.load(GamePath.obstacle[2])
    obstacle = pygame.transform.scale(obstacle, (SceneSettings.tileWidth, SceneSettings.tileHeight))
    for i in range(0,12):
        for j in range(0,MapSettings.Room_x):
            if random() > 0.9:
                mapObject[3+i][4+j+MapSettings.Room_x+MapSettings.zoulang_x] = obstacle
                bianjie.append((3+i,4+j+MapSettings.Room_x+MapSettings.zoulang_x))
    for i in range(0,12):
        for j in range(0,MapSettings.Room_x-2):
            if random() > 0.9:
                mapObject[3+i][4+j+2*MapSettings.Room_x+2*MapSettings.zoulang_x] = obstacle
                bianjie.append((3+i,4+j+2*MapSettings.Room_x+2*MapSettings.zoulang_x))          
              

    return [mapObject,bianjie]            

